<template>
  <view class="page flex-col">
    <image
      class="image_1"
      referrerpolicy="no-referrer"
      src="/static/images/login/ps1w5z7waicayv2eend8bmbgiux69mbhzqn1c2f73e8-b802-4588-8bcd-e895ded5ef42.png"
    />
	
    <image
      class="image_2"
      referrerpolicy="no-referrer"
      src="/static/images/login/ps27e3a4b3d4thdorrvorpm8ksitpv0sapje02def2e-5c07-4db6-8982-5275d01c5543.png"
    />
    <view class="block_1 flex-row">
      <image
        class="image_3"
        referrerpolicy="no-referrer"
        src="/static/images/login/9da03c69b8ae48c396f0d37c01efdeb0_mergeImage.png"
      />
       <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" style="margin-top: 15px;margin-left: 5px;"/>
    </view>
    <view class="block_2 flex-row">
      <image
        class="image_4"
        referrerpolicy="no-referrer"
        src="/static/images/login/478fbfe168504cca9a0209d64564164c_mergeImage.png"
      />
       <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" style="margin-top: 15px;margin-left: 5px;" />
    </view>
	<view class="input-item flex align-center" style="width: 10%;margin: 0px;" v-if="captchaEnabled">
	        <view class="iconfont icon-code icon"></view>
	        <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
	        <view class="login-code"> 
	          <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
	        </view>
	      </view>
    <image
      class="image_5"
      referrerpolicy="no-referrer"
      src="/static/images/login/pszqdgltb35pd7ud0x81ebwianl7szeksbe455660aa-6108-42cd-9b2b-27430ef7f9f6.png" @click="handleLogin"
    />
    <text class="text_3">忘记密码?</text>
    <image
      class="image_6"
      referrerpolicy="no-referrer"
      src="/static/images/login/ps7f13s6zvf9ghx1k2t8g9uhi80modxd2vn1c9fc9b3-0557-492a-88ce-f1410fbdb599.png"
    />
  </view>
</template>
<script>
  import { getCodeImg } from '@/api/login'

  export default {
    data() {
      return {
        codeUrl: "",
        captchaEnabled: true,
        // 用户注册开关
        register: false,
        globalConfig: getApp().globalData.config,
        loginForm: {
          username: "admin",
          password: "admin@123456",
          code: "",
          uuid: ''
        }
      }
    },
    created() {
      this.getCode()
    },
    methods: {
      // 用户注册
      handleUserRegister() {
        this.$tab.redirectTo(`/pages/register`)
      },
      // 隐私协议
      handlePrivacy() {
        let site = this.globalConfig.appInfo.agreements[0]
        this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
      },
      // 用户协议
      handleUserAgrement() {
        let site = this.globalConfig.appInfo.agreements[1]
        this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
      },
      // 获取图形验证码
      getCode() {
        getCodeImg().then(res => {
          this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
          if (this.captchaEnabled) {
            this.codeUrl = 'data:image/gif;base64,' + res.img
            this.loginForm.uuid = res.uuid
          }
        })
      },
      // 登录方法
      async handleLogin() {
        if (this.loginForm.username === "") {
          this.$modal.msgError("请输入您的账号")
        } else if (this.loginForm.password === "") {
          this.$modal.msgError("请输入您的密码")
        } else if (this.loginForm.code === "" && this.captchaEnabled) {
          this.$modal.msgError("请输入验证码")
        } else {
          this.$modal.loading("登录中，请耐心等待...")
          this.pwdLogin()
        }
      },
      // 密码登录
      async pwdLogin() {
        this.$store.dispatch('Login', this.loginForm).then(() => {
          this.$modal.closeLoading()
          this.loginSuccess()
        }).catch(() => {
          if (this.captchaEnabled) {
            this.getCode()
          }
        })
      },
      // 登录成功后，处理函数
      loginSuccess(result) {
        // 设置用户信息
        this.$store.dispatch('GetInfo').then(res => {
          this.$tab.reLaunch('/pages/index')
        })
      }
    }
  }
</script>
<style lang='css'>
@import '../static/common/common.css';
@import '../static/scss/login/index.rpx.css';
</style>
